$(function () {
    //  按钮生成器
    generateButton($('#north'));
    let columns = [[
        {field: 'bitemId', title: '主键', width: 60, align: 'center', halign: 'center', sortable: true,hidden: true},
        {field: 'bname', width: 200, title: '原因名称', align: 'center', halign: 'center', sortable: true, editor: {type: 'textbox', options: {required: true}}},
        {field: 'bmemo', width: 100, title: '备注', align: 'center', halign: 'center', sortable: true, editor: {type: 'textbox', options: {required: false}}},
        {field: 'border', width: 100, title: '排序', align: 'center', halign: 'center', sortable: true, editor: {type: 'textbox', options: {required: false}}},

    ]];
    setDatagridColumnOpts($('body').data('menu_id'), $('#e_grid'), columns, edatagrid);
    bindEnterEvents();

    // $.ajax({
    //     url: 'defectivegoods/listQuery',
    //     type: 'GET', // 或者 'POST'，取决于后端接口要求
    //     success: function(response) {
    //         // 处理返回的数据
    //         console.log('待处理原因数据:', response);
    //     },
    //     error: function(xhr, status, error) {
    //         // 处理错误
    //         console.error('请求失败:', error);
    //     }
    // });

    // $.uploadComponent({
    //     url: 'defectivegoods/upload',
    //     method: 'POST',
    //     fields: columns[0],
    //     onSuccess: function (data) {
    //         if (data.success) {
    //             $.uploadComponent('close');
    //             $('#e_grid').edatagrid('reload');
    //             $.messager.show({title: '提示', msg: data.message});
    //         } else {
    //             $(this).edatagrid('cancelRow');
    //             $(this).edatagrid('editRow', index);
    //             $.messager.alert('提示', '上传失败!' + data.message, 'error');
    //         }
    //     }
    // })



})

function edatagrid(columns) {
    $('#e_grid').edatagrid({
        method: 'GET',
        param: {},
        url: 'defectivegoods/listQuery',
        saveUrl: 'defectivegoods/insertSelective',
        updateUrl: 'defectivegoods/updateSelective',
        // destroyUrl: 'defectivegoods/deleteByPk',
        idField: 'bitemId',
        destroyMsg: {
            norecord: {
                title: '警告',
                msg: '未选中任何数据'
            },
            confirm: {
                title: '确认',
                msg: '确定删除吗?删除后不可恢复!'
            }
        },
        loadFilter: pageFilter,
        pagination: true,
        columns: columns,
        // pagePosition: 'top',
        pageSize: 200,
        pageNumber: 1,
        pageList: [200, 400, 600, 800, 1000],
        striped: true,
        fit: true,
        sortable: true,
        rownumbers: true,
        remoteSort: false,
        showFooter: true,
        singleSelect: true,
        onLoadSuccess: function (data) {
            console.log(data)
            $(this).datagrid('enableFilter');
            $(this).data('edatagrid').options.editIndex = -1;
            $('.easyui-linkbutton').each(function () {
                if ($(this).linkbutton('options').text == '查询') {
                    $(this).linkbutton('enable');
                }
            })
        },
        onLoadError: function () {
            $('.easyui-linkbutton').each(function () {
                if ($(this).linkbutton('options').text == '查询') {
                    $(this).linkbutton('enable');
                }
            })
        },
        onBeforeLoad: function (param) {
            $(this).datagrid('destroyFilter');
            $('.easyui-linkbutton').each(function () {
                if ($(this).linkbutton('options').text == '查询') {
                    $(this).linkbutton('disable');
                }
            })
            return true;
        },
        onSuccess: function (index, row) {
            $(this).edatagrid('reload');
            if (row.success) {
                $.messager.show({title: '提示', msg: row.message});
            } else {
                $(this).edatagrid('enableEditing');
                $.messager.alert('提示', row.message, 'warning')
            }
        },
        onDestroy: function (index, row) {
            btn_delete_grid(row);
            $(this).edatagrid('reload');
            // if (row.success) {
            //     // $.messager.show({title: '提示', msg: row.message});
            // } else {
            //     $.messager.alert('提示', row.message, 'warning')
            // }
        },
        onError: function (index, row) {
            $.messager.show({title: '提示', msg: row.message});
        },
        onResizeColumn: function (f, w) {
            columnResizeEvent($('body').data('menu_id'), f, w);
        },
    })
}

function btn_search() {
    $('#e_grid').edatagrid('reload');
}

function btn_add() {
    $('#e_grid').edatagrid('addRow');
}

function btn_modify() {
    let selected = $('#e_grid').edatagrid('getSelected');
    if (selected != undefined) {
        let index = $('#e_grid').edatagrid('getRowIndex', selected);
        if (index != -1) {
            $('#e_grid').edatagrid('editRow', index);
        }
    } else {
        $.messager.alert('提示', '点击鼠标左键选中行后再执行本功能', 'warning');
    }
}

function btn_delete() {
    let selected = $('#e_grid').edatagrid('getSelected');
    if (selected != undefined) {
        let index = $('#e_grid').edatagrid('getRowIndex', selected);
        if (index != -1) {
            $('#e_grid').edatagrid('destroyRow', index);
        }
    } else {
        $.messager.alert('提示', '点击鼠标左键选中行后再执行本功能', 'warning');
    }
}

function btn_delete_grid(selected){
    //写一个完整的ajax方法
    $.ajax({
        url: 'defectivegoods/deleteByPk',
        type: 'POST',
        data: {bitemId:selected.bitemId},
        success: function (data) {
            if (data.success) {
                $.messager.show({title: '提示', msg: data.message});
            } else {
                $.messager.alert('提示', data.message, 'warning')
            }
        },
    })

}


//  导入Excel
function btn_import() {
    $.uploadComponent('open');
}

function btn_save() {
    $('#e_grid').edatagrid('saveRow');
}

function btn_export() {
    exportExcel('#e_grid', '待处理原因');
}